<?php

global $_W, $_GPC;

$act = $_GPC['act'];

// 列表
if ($act == 'list') {
    $sql = "select * from " . tablename('weidun_card_tips') . " order by rand() limit 10";
    $list = pdo_fetchall($sql);
    $openid = $_W['openid'];
    if (!empty($list)) {
        foreach ($list as $key => $value) {
            $condition = array('openid' => $openid, 'tips_id' => $value['id']);

            // 是否收藏
            $haveReceive = M('tips_receive')->getOne($condition);
            $list[$key]['haveReceive'] = !empty($haveReceive) ? 1 : 0;
            // 是否点赞
            $havePraise = M('tips_praise')->getOne($condition);
            $list[$key]['havePraise'] = !empty($havePraise) ? 1 : 0;
        }
    }

    $this->result(0, '获取到信息', $list);
}

// 收藏
if ($act == 'receive') {
    $info = M('tips_receive')->getOne(
        array(
            'openid' => $_W['openid'],
            'tips_id' => $_GPC['id']
        )
    );

    if (!$info) {
        $data['openid'] = $_W['openid'];
        $data['tips_id'] = $_GPC['id'];
        $data['create_time'] = time();

        if (!empty($data['openid']) && !empty($data['tips_id'])) {
            M('tips_receive')->update($data);
            M('tips')->update(array('receive_count +=' => 1, 'id' => $data['tips_id']));
        } else {
            $res = array();
        }
    }

    $this->result(0, '收藏成功', array());
}

// 点赞
if ($act == 'like') {
    $info = M('tips_praise')->getOne(
        array(
            'openid' => $_W['openid'],
            'tips_id' => $_GPC['id']
        )
    );
    if (!$info) {
        $data['openid'] = $_W['openid'];
        $data['tips_id'] = $_GPC['id'];
        $data['create_time'] = time();
        if (!empty($data['openid']) && !empty($data['tips_id'])) {
            M('tips_praise')->update($data);
            M('tips')->update(array('praise_count +=' => 1, 'id' => $data['tips_id']));
        } else {
            $res = array();
        }
    }
    $this->result(0, '点赞成功', array());
}

// 用户收藏列表
if ($act == 'userReceive') {
    $openid = $_W['openid'];
    $where = array('openid' => $openid);
    // 用户信息
    $user = mc_fansinfo($_W['openid']);

    $fields = array('*');
    $keyField = '';
    $orderBy = array('id desc');

    $page = max(1, $_GPC['page']);
    $pageSize = max($_GPC['pageSize'], 20);
    $limit = array($page, $pageSize);

    $list = M('tips_receive')->getAll($where, $fields, $keyField, $orderBy, $limit);
    $return = array();
    if (!empty($list)) {
        foreach ($list as $key => $value) {
            $tip = M('tips')->getInfo($value['tips_id']);
            $return[$key]['title'] = $tip['title'];
            $return[$key]['content'] = $tip['content'];
            $return[$key]['praiseCount'] = $tip['praise_count'];
            $return[$key]['receiveCount'] = $tip['receive_count'];
        }
    }

    $this->result(0, '获取成功', $return);
}

// 用户点赞列表
if ($act == 'userLike') {
    $openid = $_W['openid'];
    $where = array('openid' => $openid);
    // 用户信息
    $user = mc_fansinfo($_W['openid']);

    $fields = array('*');
    $keyField = '';
    $orderBy = array('id desc');

    $page = max(1, $_GPC['page']);
    $pageSize = max($_GPC['pageSize'], 20);
    $limit = array($page, $pageSize);

    $list = M('tips_praise')->getAll($where, $fields, $keyField, $orderBy, $limit);
    $return = array();
    if (!empty($list)) {
        foreach ($list as $key => $value) {
            $tip = M('tips')->getInfo($value['tips_id']);
            $return[$key]['title'] = $tip['title'];
            $return[$key]['content'] = $tip['content'];
            $return[$key]['praiseCount'] = $tip['praise_count'];
            $return[$key]['receiveCount'] = $tip['receive_count'];
        }
    }

    $this->result(0, '获取成功', $return);
}